﻿using CinemaMaestro.DataLayer.Contracts.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CinemaMaestro.ViewModels
{
    public class MovieViewModel
    {
        public MovieViewModel()
        {
            this.Categories = new HashSet<CategoryViewModel>();
            this.Actors = new HashSet<ActorViewModel>();
            this.Directors = new HashSet<DirectorViewModel>();
        }

        public int Id { get; set; }
        public string Title { get; set; }
        public int Duration { get; set; }
        public string Description { get; set; }
        public DateTime ReleaseDate { get; set; }
        public byte[] Image { get; set; }

        public virtual ICollection<CategoryViewModel> Categories { get; set; }
        public virtual ICollection<ActorViewModel> Actors { get; set; }
        public virtual ICollection<DirectorViewModel> Directors { get; set; }

        public static implicit operator MovieViewModel(Movie movie)
        {
            return new MovieViewModel
            {
                Id = movie.Id,
                Title = movie.Title,
                Duration = movie.Duration,
                Description = movie.Description,
                ReleaseDate = movie.ReleaseDate,
                Image = movie.Image,
                Categories = ConvertToCategoryViewModelList(movie.Categories),
                Actors = ConvertToActorViewModelList(movie.Actors),
                Directors = ConvertToDirectorViewModelList(movie.Directors)
            };
        }

        public static implicit operator Movie(MovieViewModel movieVM)
        {
            return new Movie
            {
                Id = movieVM.Id,
                Title = movieVM.Title,
                Duration = movieVM.Duration,
                Description = movieVM.Description,
                ReleaseDate = movieVM.ReleaseDate,
                Image = movieVM.Image,
                Categories = ConvertToCategoryList(movieVM.Categories),
                Actors = ConvertToActorList(movieVM.Actors),
                Directors = ConvertToDirectorList(movieVM.Directors)
            };
        }

        private static List<DirectorViewModel> ConvertToDirectorViewModelList(ICollection<Director> directors)
        {
            List<DirectorViewModel> list = new List<DirectorViewModel>();
            foreach (Director director in directors)
            {
                DirectorViewModel directorVM = director;
                list.Add(directorVM);
            }
            return list;
        }

        private static List<Director> ConvertToDirectorList(ICollection<DirectorViewModel> directors)
        {
            List<Director> list = new List<Director>();
            foreach (DirectorViewModel directorVM in directors)
            {
                Director director = directorVM;
                list.Add(director);
            }
            return list;
        }

        private static List<ActorViewModel> ConvertToActorViewModelList(ICollection<Actor> actors)
        {
            List<ActorViewModel> list = new List<ActorViewModel>();
            foreach (Actor actor in actors)
            {
                ActorViewModel actorVM = actor;
                list.Add(actorVM);
            }
            return list;
        }

        private static List<Actor> ConvertToActorList(ICollection<ActorViewModel> actors)
        {
            List<Actor> list = new List<Actor>();
            foreach (ActorViewModel actorVM in actors)
            {
                Actor actor = actorVM;
                list.Add(actor);
            }
            return list;
        }

        private static List<CategoryViewModel> ConvertToCategoryViewModelList(ICollection<Category> categories)
        {
            List<CategoryViewModel> list = new List<CategoryViewModel>();
            foreach (Category category in categories)
            {
                CategoryViewModel categoryVM = category;
                list.Add(categoryVM);
            }
            return list;
        }

        private static List<Category> ConvertToCategoryList(ICollection<CategoryViewModel> categories)
        {
            List<Category> list = new List<Category>();
            foreach (CategoryViewModel categoryVM in categories)
            {
                Category category = categoryVM;
                list.Add(category);
            }
            return list;
        }

        public override string ToString()
        {
            return Title;
        }

    }
}
